<?php
namespace Home\Controller;
use Think\Controller;
use Think\Upload;

class IndexController extends Controller {
	
	public function __construct(){
		parent::__construct();
		
		if(empty($_SESSION['user']))
		{
			 $this->error('对不起,您还没有登录!请先登录!', U('Login/login'), 1);
			
		}
	}
	
    public function index(){
	  	$name = $_SESSION['user'];
		$account = M("account")->where(array('username'=>$name))->find();

		$logo = M('web_config');
		$logo_path = $logo->field('logo_img')->find(1);
		
		$this->assign([
			'account' => $account,
			'logo_path' => $logo_path,
		]);
		$this->display();
    }
	
	
	
	public function down_tools(){
		$ads=M('ads');
		if(IS_POST){
			$data = I('post.','','htmlspecialchars');
			if(!is_file('/admin/'.$data['cover'])){
				$upload = new \Think\Upload();
				//$upload->maxSize   =     3145728 ;// 设置附件上传大小
			    $upload->exts      =     array('jpg', 'jpeg','bmp','png');// 设置附件上传类型
			    $upload->rootPath  =     APP_PATH; // 设置附件上传根目录
			    $upload->savePath  =     '/Public/ads/'; // 设置附件上传（子）目录
			    $upload->subName   =     array('date','Ymd');
			    $info   =   $upload->upload();
			    
			    /*if(!$info) {// 上传错误提示错误信息
	       		 	$this->error($upload->getError());

	   	    	}*/

	   	    	$data['cover'] = $info['cover']['savepath'].$info['cover']['savename'];

	   	    	//上传成功则 录入其它信息
   	    	}else{
   	    		$data['cover']=I('post.cover');
   	    	}
   	   
   	   		if($data['id']){
   	   			
   	   			$res = $ads->save($data);
   	   		}else{
   	   			$res = $ads->add($data);
   	   		}
   	    	
   	    	if($res){
   	    		$this->success('添加成功',U('ads'),2);
   	    	}else{
   	    		$this->error('添加失败','',2);
   	    	}


		}

		$res=$ads->select();
		$this->assign('res',$res);

		
		$this->display();
	}

	public function ads_del($id){
		M('ads')->delete($id);
		$this->success('成功删除广告',U('ads'));
	}
	
	public function logout() {
		session_unset($_SESSION['user']);
		session_destroy();
//		echo "<script>alert('退出成功!');top.location.href='http://192.168.0.130/index.php/Home/Login/login.html';</script>";
		echo "<script>alert('退出成功!');top.location.href='".U('Login/login')."';</script>";
//		$this->error('对不起,您还没有登录!请先登录!', U('Login/login'), 1);
	}

	//技术信息页面
	public function tec(){
		
		if(I('post.')){
			$data = I('post.');
			if($data['id']){  //如有id ， save
				$res = M('tec_info')->save($data);
				if($res!==false){
					$this->success('更新成功',U('tec'));
				}else{
					$this->error('更新失败，请稍后再试');
				}
			}else{  //没id 则 add
				$res = M('tec_info')->add($data);
				if($res!==false){
					$this->success('更新成功',U('tec'));
				}else{
					$this->error('更新失败，请稍后再试');
				}
			}
		}
		$getTecInfo =  M('tec_info')->find(1);  //只有1条记录
		$this->assign('getTecInfo',$getTecInfo);
		$this->display();
	}

	//客户留言
	public function comments(){

		$comm = M('message');
	

		//评论通过审核，上架
		if(I('get.passid')){
			$passid = I('get.passid');

			$passcomm = $comm->save(['id'=>$passid,'is_show'=>1]);
			if($passcomm!==false){
				$this->success('评论可见');
			}else{
				$this->error('数据库问题');
			}
		}

		//删除评论
		if(I('get.delid')){
			$delid = I('get.delid');
			$delcomm = $comm->delete($delid);
			if($delcomm){
				$this->success('已删除评论');
			}else{
				$this->error('删除评论失败');
			}
		}

		//评论下架
		if(I('get.nopassid')){
			$nopassid = I('get.nopassid');
			$nopasscomm = $comm->save(['id'=>$nopassid,'is_show'=>0]);
			if($nopasscomm!==false){
				$this->success('评论已下架，不再显示');
			}else{
				$this->error('评论下架失败');
			}
		}


		$res = $comm->select();
		//dump($res);die;
		$this->assign('res',$res);
		$this->display();
	}


	//banner图上传
    public function ads(){

	    //dump($_FILES);die;
        $model = M('banners');


        if($_FILES){
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     50000 ;// 设置附件上传大小
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =     ROOT_PATH.'/Public/Uploads/'; // 设置附件上传根目录
            $upload->savePath  =     ''; // 设置附件上传（子）目录
            // 上传文件
            $info   =   $upload->upload();
            //dump($info);die;
            if(!$info) {// 上传错误提示错误信息
                //dump($info);die;
                $this->error($upload->getError());
            }else{// 上传成功

                foreach($info as $k){


                    $data[$k['key']] = '/Public/Uploads/'.$k['savepath'].$k['savename'];
                    //key 组件的名字， 也是数据库字段名。 savepath+savename= 图片相对路径

                }
                $data['id']=1;
                //dump($data);die;
                $res = $model->save($data);
                //echo $model->_sql();die;
                if($res!==false){
                    $this->success('成功更新！','ads');
                }else{
                    $this->error('服务忙， 请稍后再试');
                }
            }
        }

        $res = $model->find(1);
        $this->assign('res',$res);
	    $this->display();
    }


	
	
}
















